package pers.xhh.frontapi.util;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

/**
 * @description:
 * @author: wei-xhh
 * @create: 2020-11-08
 */
public class FileUtil {
    public static boolean downloadFile(String url, String saveAddress) {
        try {
            URL fileUrl = new URL(url);
            InputStream is = fileUrl.openStream();
            OutputStream os = new FileOutputStream(saveAddress);
            byte bf[] = new byte[1024];
            int length = 0;
            while ((length = is.read(bf, 0, 1024)) != -1) {
                os.write(bf, 0, length);
            }
            is.close();
            os.flush();
            os.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean writeFile(InputStream is, String outPutName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outPutName);
            byte bf[] = new byte[1024];
            int length = 0;
            while ((length = is.read(bf, 0, 1024)) != -1) {
                fos.write(bf, 0, length);
            }
            is.close();
            fos.flush();
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
